package com.abu.edums.controller.front;

import com.abu.edums.service.TeacherService;
import com.abu.pojo.Course;
import com.abu.pojo.Teacher;
import com.abu.utils.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("eduservice/teacherfront")
public class TeacherFrontController {

    @Autowired
    private TeacherService teacherService;

    @PostMapping("getTeacherFrontList/{page}/{limit}")
    public Resp queryPageByPage(@PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        Map<String, Object> map = this.teacherService.queryPage(page, limit);
        return Resp.ok().data(map);
    }

    /**
     * 查询讲师和讲师的相关课程
     *
     * @param teacherId
     * @return
     */
    @GetMapping("getTeacherFrontInfo/{teacherId}")
    public Resp getTeacherFrontInfo(@PathVariable("teacherId") String teacherId) {
//        将课程和讲师放到map中
        Map<String, Object> map = this.teacherService.queryTeacherAndCourses(teacherId);
        if (CollectionUtils.isEmpty(map)) {
            return Resp.error();
        }
        Teacher teacher = (Teacher) map.get("teacher");
        List courseList = (List<Course>) map.get("courseList");
        map.put("teacher", teacher);
        map.put("courseList", courseList);
        return Resp.ok().data(map);
    }
}
